/***************************************************************************
 *   oboe.controllers.jml.helpers.JMLSlideshow                             *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml.helpers
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import oboe.controllers.jml.events.JMLEvent;
	import oboe.utils.Prototype;
	
	public class JMLSlideshow extends Sprite
	{
		public var stream:String;
		private var loader:URLLoader;
		public var src:Array;
		private var imgs:Vector.<Loader>;
		public var currentImage:int;
		
		public var fadeOutImage:Loader;
		public var maxImageTime:Number = 5;
		private var currentImageDisplayTime:Number;
		public var autoPlay:Boolean = true;
		
		public var maxSize:Number
		
		public function JMLSlideshow()
		{
			super();
			this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
			this.currentImage = -1;
			this.currentImageDisplayTime = 0;
		}
		
		private function onComplete( e:JMLEvent ):void
		{
			if( stream )
			{
				this.loader = new URLLoader();
				this.loader.addEventListener(Event.COMPLETE, onGotStream );
				this.loader.load( new URLRequest( stream ) );
			}
			else if( src )
			{
				this.loadImages();
			}
			Object(this).addCallback( Event.ENTER_FRAME, onTick, 0.013 );
		}
		
		private function onGotStream( e:Event ):void
		{
			this.src = JSON.decode( this.loader.data )[0] as Array;
			this.loadImages();
		}
		
		private function loadImages():void
		{
			imgs = new Vector.<Loader>( this.src.length, true );
			this.currentImageDisplayTime = this.maxImageTime;
		}
		
		public function onTick( time:Number ):void
		{
			if( this.fadeOutImage && this.fadeOutImage.parent == this )
			{
				var alpha:Number = this.fadeOutImage.alpha - time;
				if( alpha <= 0 )
				{
					alpha = 0;
					this.removeChild( this.fadeOutImage );
					this.fadeOutImage.alpha = 1;
					this.fadeOutImage = null;
				}
				else
				{
					this.fadeOutImage.alpha = alpha;
				}
				this.imgs[ this.currentImage ].alpha = 1-alpha;					
			}
			
			if( !imgs || !imgs.length ) return;
			
			this.currentImageDisplayTime += time;
			if( this.currentImageDisplayTime > this.maxImageTime )
			{
				if( this.currentImage > -1 )
				{
					if( this.fadeOutImage && this.fadeOutImage.parent == this )
					{
						this.removeChild( this.fadeOutImage );
						this.fadeOutImage.alpha = 1;
					}
					this.fadeOutImage = this.imgs[ this.currentImage ];
				}
				this.currentImage = (this.currentImage+1)%imgs.length;
				
				if( !this.imgs[ this.currentImage ] )
				{
					var ldr:Loader = this.imgs[ this.currentImage ] = new Loader();
					Prototype.addCallbackEx( ldr.contentLoaderInfo, Event.COMPLETE, onLoadingComplete, this.currentImage ); 
					Object(ldr).loadEx( new URLRequest( src[ this.currentImage ] ) );
					this.currentImageDisplayTime = -1;
				}
				else if( this.imgs[ this.currentImage ].contentLoaderInfo.content )
				{
					this.onLoadingComplete( null, this.currentImage );
					this.currentImageDisplayTime = 0;
				}
			}
		}
		
		private function onLoadingComplete( e:Event, id:int ):void
		{
			if( id != this.currentImage ) return;
			Bitmap( this.imgs[ id ].content ).smoothing = true;
			var img:DisplayObject = this.imgs[ id ];
			var max:int = Math.max( img.width, img.height );
			img.scaleX = img.scaleY *= maxSize/max;
			img.x = maxSize/2 - img.width/2;
			img.y = maxSize/2 - img.height/2;
			this.addChild( img );
			if( this.fadeOutImage )	img.alpha = 0;
		}
	}
}